
var bsurl = require('../../utils/baseUrl.js');

var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    identity: 0,
    userName: "",
    pwd: ""
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: "客户登入",
    })
  },
  textinput: function (event) {
    var type = event.currentTarget.dataset.type;
    if (type == 1) {
      this.setData({
        userName: event.detail.value
      })
    } else {
      this.setData({
        pwd: event.detail.value
      })
    }
  },
  login: function () {
    wx.showToast({
      title: '登入中...',
      icon: "loading"
    })

    var that = this;
    wx.request({
      url: bsurl + "user/login.do",
      method: "post",
      data: {
        body: {
          username: this.data.userName,    
          userpwd: this.data.pwd,
          identity:this.data.identity
        }
      },
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.hideToast();
        if (res.statusCode != 200) {
          wx.showModal({
            title: '提示',
            content: "请求失败",
          });
          return;
        }
        console.log(res);
        if (res.data.success) {
          app.globalData.userInfo = res.data.body;
          app.globalData.JSESSIONID = res.header.JSESSIONID;
          app.globalData.identity = that.data.identity;
          var newUrl = '../user/home/home';
          if (that.data.identity){
            newUrl = '../shop/home/home';
          }
          wx.redirectTo({
            url: newUrl,
          })
        }
        else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
          });
        }
      },
      fail: function (res) {
        wx.hideToast();
        wx.showModal({
          title: '提示',
          content: "网络请求失败",
        });
      }
    })
  },
  radioChange: function (e) {
    this.setData({
      identity: Number(e.detail.value)
    });
    var str = "";
    if (e.detail.value == "0")
      str = "客户登入";
    else {
      str = "商家登入";
    }
    wx.setNavigationBarTitle({
      title: str,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  }


})